package cn.tedu.luban.worker.main.controller;

import cn.tedu.luban.worker.common.enums.AuditStatusEnum;
import cn.tedu.luban.worker.main.converter.WorkerAuditConverter;
import cn.tedu.luban.worker.main.converter.WorkerConverter;
import cn.tedu.luban.worker.main.service.WorkerAdminService;
import cn.tedu.luban.worker.protocol.VO.AdminAuditDetailVO;
import cn.tedu.luban.worker.protocol.VO.AdminWorkerListVO;
import cn.tedu.luban.worker.protocol.bo.AdminWorkerBO;
import cn.tedu.luban.worker.protocol.param.AuditSubmitParam;
import cn.tedu.luban.worker.protocol.query.WorkerListQuery;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.exception.BusinessException;
import com.tedu.inn.protocol.model.Result;
import com.tedu.inn.protocol.pager.PagerResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.util.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@Api(
    value = "师傅服务后台管理接口",
    tags = "师傅服务后台管理接口"
)
public class WorkerAdminController {
    @Autowired
    private WorkerConverter workerConverter;
    @Autowired
    private WorkerAdminService workerAdminService;
    @Autowired
    private WorkerAuditConverter workerAuditConverter;
    /**
     * 师傅审核列表 分页查询
     */
    @PostMapping("admin/worker/aduit")
    @ApiOperation(value = "审核列表")
    public Result<PagerResult<AdminWorkerListVO>> workerAuditList(@RequestBody WorkerListQuery workerListQuery)
        throws BusinessException{
        //专门给pagerResult使用的 自定义的bo对象
        ListRecordTotalBO<AdminWorkerBO> workerBOS=workerAdminService.workerAuditList(workerListQuery);
        //转化器,assemble
        PagerResult<AdminWorkerListVO> pagerResult = workerConverter.bo2PagerResultVO(workerBOS, workerListQuery);
        log.info("pagerResult:{}",pagerResult);
        return new Result<>(pagerResult);
    }
    /**
     * 审核列表 某个师傅的审核详情
     */
    @PostMapping("admin/worker/audit/detail")
    @ApiOperation(value = "审核详情")
    public Result<AdminAuditDetailVO> auditDetail(@RequestParam("workerId") Long userId)
        throws BusinessException{
        //从业务层 查询BO
        AdminWorkerBO adminWorkerBO = workerAdminService.auditDetail(userId);
        //转化vo
        AdminAuditDetailVO adminAuditDetailVO =
                workerAuditConverter.bo2vo(adminWorkerBO);
        //封装result
        return new Result<>(adminAuditDetailVO);
    }
    /**
     * 师傅审核提交
     */
    @PostMapping("/admin/worker/audit/save")
    @ApiOperation(value = "审核提交")
    public Result<Long> auditSave(@RequestBody AuditSubmitParam auditSubmitParam)
        throws BusinessException{
        Long auditId=workerAdminService.auditSave(auditSubmitParam);
        return new Result<>(auditId);
    }
    @PostMapping(value = "admin/worker/search")
    @ApiOperation(value = "师傅列表")
    public Result<PagerResult<AdminWorkerListVO>> queryWorkers(@RequestBody WorkerListQuery workerListQuery) {
        workerListQuery.setAuditStatus(Collections.singletonList(AuditStatusEnum.APPROVED.getValue()));
        ListRecordTotalBO<AdminWorkerBO> workerListTotalRecord = this.workerAdminService.queryWorker(workerListQuery);
        return new Result(this.workerConverter.bo2PagerResultVO(workerListTotalRecord, workerListQuery));
    }
    @PostMapping("admin/worker/enable" )
    @ApiOperation(value = "师傅启用")
    @ApiImplicitParam(name = "id",value = "师傅 ID",dataType = "Long",required = true)
    public Result<Integer> enableWorker(@RequestParam Long id) throws BusinessException {
        return new Result(this.workerAdminService.enableWorker(id));
    }
    @PostMapping("admin/worker/disable")
    @ApiOperation(value = "师傅禁用")
    @ApiImplicitParam(name = "id",value = "师傅 ID",dataType = "Long",required = true)
    public Result<Integer> disableWorker(@RequestParam Long id) throws BusinessException {
        return new Result(this.workerAdminService.disableWorker(id));
    }
}

























